<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>书籍信息数据</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card class="box-card">
            <el-row :gutter="5">
                <el-col :span="7">
                    <el-input placeholder="搜索书籍名称或图书号" v-model="select" clearable @change="initData"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-button type="primary" icon="el-icon-search" @click="initData">搜索</el-button>
                </el-col>
            </el-row>



            <el-row>
                <el-table :data="data" stripe border style="width: 100%">
                    <el-table-column type="expand">

                    <template slot-scope="scope">
                        <el-row>
                            <el-table :data="scope.row.children" style="width: 100%">
                                <el-table-column prop="id" label="ID">
                                </el-table-column>
                                <el-table-column  prop="bookName" label="书名">
                                </el-table-column>
                                <el-table-column  prop="bookState" label="是否可借">
                                    <template slot-scope="scope">
                                        <el-switch v-model="scope.row.bookState" active-color="#13ce66" inactive-color="#ff4949" @change="changeSwitch2(scope.row)">
                                        </el-switch>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="user" label="借书人">
                                </el-table-column>
                                <el-table-column prop="phone" label="电话">
                                </el-table-column>
                                <el-table-column prop="outTime" label="借书时间">
                                </el-table-column>
                                <el-table-column prop="detailed" label="描述">
                                </el-table-column>
                            </el-table>
                        </el-row>
                    </template>

                    </el-table-column>
                    <el-table-column type="index" label="#">
                    </el-table-column>
                    <el-table-column prop="bookname" label="书名">
                    </el-table-column>
                    <el-table-column prop="sort" label="类型">
                    </el-table-column>
                    <el-table-column prop="count" label="剩余数量">
                    </el-table-column>
                    <el-table-column prop="price" label="租借价格">
                    </el-table-column>
                    <el-table-column label="当前状态">
                        <template slot-scope="scope">
                            <el-switch v-model="scope.row.state" active-color="#13ce66" inactive-color="#ff4949" @change="changeSwitch(scope.row)">
                            </el-switch>
                        </template>
                    </el-table-column>

                    
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="getdate(scope.row)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(scope.row)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-row>

            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                @prev-click="headlePrev" @next-click="headleNext" :current-page="2" :page-sizes="[3, 5, 7, 8]"
                :page-size="limit" layout="total, sizes, prev, pager, next, jumper" :total="toto">
            </el-pagination>


            <!-- 修改 -->
            <el-dialog title="修改信息" :visible.sync="dialogUpdata">
                <el-form :model="updateUser" ref="updataFormRef" :rules="formRules" label-width="80px">
                    <el-form-item label="书籍编号" >
                        <el-input v-model="updateUser.id" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="书籍名称" prop="bookname">
                        <el-input v-model="updateUser.bookname"></el-input>
                    </el-form-item>
                   <el-form-item label="书籍类型">
                      <el-select v-model="updateUser.sort" placeholder="请选择活动区域">
                        <el-option label="科幻类" value="科幻类"></el-option>
                        <el-option label="儿童类" value="儿童类"></el-option>
                        <el-option label="哲学类" value="哲学类"></el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="书籍数量">
                        <el-input-number v-model="updateUser.count" :min="0" :max="10" label="描述文字"></el-input-number>
                    </el-form-item>

                    <el-form-item label="书籍价格" prop="price">
                        <el-input v-model="updateUser.price"></el-input>
                    </el-form-item>


                    
                </el-form>




                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogUpdata = false">取 消</el-button>
                    <el-button type="primary" @click="update">确 定</el-button>
                </span>
            </el-dialog>



        </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                select: "",
                page: 1,//页数
                limit: 7,//每页条数
                toto: 0,//查询到的总数
                data: [],
                dialogUpdata: false,
                updateUser: [],
                formRules:{
                    bookname:[
                        { required : true ,message : '请输入书籍名称',trigger : "blur"},
                        { min : 1,max : 30 , message : '最少一个任意字符' , trigger: "blur"}
                    ],
                    price:[
                        { required : true ,message : '请输入书籍价格',trigger : "blur"},
                        { type:"number", message : '请输入数字'}
                    ]


                    
                }
            };
        },

        mounted() {
            this.initData()

        },

        methods: {
            initData() {
                let url = `book?_page=${this.page}&_limit=${this.limit}`;
                if (this.select) {
                    url = `book?_page=${this.page}&_limit=${this.limit}&q=${this.select}`
                }
                this.$http.get(url).then((rs) => {
                    this.data = rs.data
                    this.toto = parseInt(rs.headers['x-total-count']);
                })
            },//获取当前要修改行的数据
            getdate(row) {
                this.dialogUpdata = true
                this.updateUser = row
            },
            update() {
                this.$refs.updataFormRef.validate(valid=>{
                    if(!valid) return false
                    this.$http.put("book/" + this.updateUser.id, this.updateUser).then(rs => {
                    if (rs.status == 200) {
                        this.$message.success("添加成功！")
                        this.dialogUpdata = false
                    } else {
                        this.$message.error("修改失败！")
                        this.dialogUpdata = false
                    }
                })
                })
               
            },
            //开关
            changeSwitch(row) {
                this.$http.put("book/"+row.id,row).then(rs=>{
                    if(rs.status==200){
                        this.$message.success("状态修改成功");
                    }else{
                        this.$message.error("状态修改失败");
                    }
                })
            },
            changeSwitch2(row) {
                this.$http.put("book/children/"+row.id,row).then(rs=>{
                    if(rs.status==200){
                        this.$message.success("状态修改成功");
                    }else{
                        this.$message.error("状态修改失败");
                    }
                })
            },
            del(row){
                this.$http.delete("book/"+row.id).then(rs=>{
                    if(rs.status==200){
                        this.$message.success("删除成功");
                        this.initData()
                    }else{
                        this.$message.error("删除失败");
                        this.initData()
                    }
                })
            },
            //分页改变每页数量
            handleSizeChange(size) {
                this.limit = size;
                this.initData();
            },//分页改变
            handleCurrentChange(pager) {
                this.page = pager;
                this.initData();
            },//分页上一页
            headlePrev(pager) {
                this.page = pager;
                this.initData();
            },//分页下一页
            headleNext(pager) {
                this.page = pager;
                this.initData();
            }
        },
    };
</script>

<style lang="scss" scoped>

</style>